/*
 * ProtocolHandler.java    0.0.1    24/dic/2009
 * 
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */
package org.mentalsmash.tazio.transparency.impl;

import java.io.IOException;
import java.io.Serializable;

import org.mentalsmash.tazio.commons.identifiers.NID;
import org.mentalsmash.tazio.net.cs.MessageOutputStream;
import org.mentalsmash.tazio.net.cs.server.ObjectMessageProtocolHandler;
import org.mentalsmash.tazio.net.cs.server.impl.ChannelFacade;
import org.mentalsmash.tazio.transparency.commons.messages.DisconnectionMessage;
import org.mentalsmash.tazio.transparency.commons.messages.TazioMessage;
import org.mentalsmash.tazio.transparency.interfaces.MessageFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @version 0.0.1 24/dic/2009
 * @author Enrico Savioli <savioli.enrico@gmail.com>
 */
public class ProtocolHandler extends ObjectMessageProtocolHandler {

    private final static Logger log = LoggerFactory
	    .getLogger(ProtocolHandler.class);

    private NID sourceId = null;

    private final MessageFilter msgFilter;
    
    public ProtocolHandler() {
	this.msgFilter = BaseMessageFilter.getInstanceOldStyle();
    }

    /*
     * (non-Javadoc)
     * 
     * @seeorg.mentalsmash.tazio.net.cs.server.ObjectMessageProtocolHandler#
     * handleMessage(java.io.Serializable,
     * org.mentalsmash.tazio.net.cs.MessageOutputStream)
     */
    @Override
    public void handleMessage(Serializable msgReceived, MessageOutputStream out) {

	log.debug("Received a message");

	if (msgReceived == null) {

	    log.debug("The message is null. Ignoring it.");
	    return;

	}

	log.debug("The message received is: " + msgReceived.toString());

	TazioMessage message = (TazioMessage) msgReceived;

	if (sourceId == null) {
	    sourceId = message.getMsgID().getNodeID();
	}

	log.debug("Retrieving the answer.");

	TazioMessage answer = null;
	//MessageFilter msgFilter = BaseMessageFilter.getInstanceOldStyle();
	answer = msgFilter.handleMessage(message);

	log.debug("Answer retrieved. Returning it to the sender.");
	// System.out.println("Retrieved the answer. it is: "+
	// answer.toString());

	try {
	    out.writeMessage(answer);
	} catch (IOException e) {
	    String msg = "Input/Output error while answering a message";
	    log.error(msg);
	    e.printStackTrace();
	}

	// System.out.println("Answer sent");
	log.debug("Answer sent to the recipient");

    }

    /*
     * (non-Javadoc)
     * 
     * @seeorg.mentalsmash.tazio.net.cs.server.ProtocolHandler#registered(org.
     * mentalsmash.tazio.net.cs.server.impl.ChannelFacade)
     */
    @Override
    public void registered(ChannelFacade source) {

	System.err.println("SOMEONE CONNECTED TO ME!!");

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.mentalsmash.tazio.net.cs.server.ProtocolHandler#unregistered(org.
     * mentalsmash.tazio.net.cs.server.impl.ChannelFacade)
     */
    @Override
    public void unregistered(ChannelFacade source) {
	if (sourceId != null) {
	    log.debug("source unregistered");
	    // fake message to notify disconnections
	    TazioMessage msg = new BaseTazioMessage(new BaseTazioMessageID(-1,
		    sourceId), new DisconnectionMessage(this.sourceId));
	   msgFilter.handleMessage(msg);
	}
    }

}
